<template>
  <s-modal
    title="添加"
    :visible="visible"
    width="800px"
    ok-text="保存"
    :closable="true"
    @ok="save()"
    @cancel="visible = false"
  >
    <div style="height: 400px">
      <s-scroll>
        <s-form ref="form" :value="formValue">
          <s-form-item
            label="字段个数"
            name="fieldNum"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-input-number v-model="formValue.fieldNum" :min="1" :max="50" placeholder="字段个数">
            </s-input-number>
          </s-form-item>
          <s-form-item
            label="初始下标"
            name="startIndex"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-input-number
              v-model="formValue.startIndex"
              :min="1"
              :max="50"
              placeholder="初始下标"
            >
            </s-input-number>
          </s-form-item>
          <s-form-item label="名称" name="name" :rules="[{ required: true, message: '不能为空' }]">
            <s-input v-model="formValue.name" placeholder="名称"> </s-input>
          </s-form-item>

          <s-form-item
            label="数据编码"
            name="key"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-input v-model="formValue.key" placeholder="数据编码,驼峰格式"> </s-input>
          </s-form-item>

          <s-form-item
            label="数据类型"
            name="javaType"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-select v-model="formValue.javaType" code="JAVA_TYPE" placeholder="数据类型">
            </s-select>
          </s-form-item>
          <s-form-item
            label="字段名称"
            name="dbName"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-input v-model="formValue.dbName" placeholder="数据库字段名称"> </s-input>
          </s-form-item>
          <s-form-item
            label="字段类型"
            name="dbType"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-select v-model="formValue.dbType" code="DATABASE_FIELD_TYPE" placeholder="字段类型">
            </s-select>
          </s-form-item>
          <s-form-item
            label="字段长度"
            name="dbLength"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-input-number
              v-model="formValue.dbLength"
              :min="0"
              :max="5000"
              placeholder="数据库字段长度"
            />
          </s-form-item>
          <s-form-item
            v-if="formValue.dbType == 'double'"
            label="字段精度"
            name="dbLength2"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-input-number
              v-model="formValue.dbLength2"
              :min="0"
              :max="100"
              placeholder="数据库字段精度"
            />
          </s-form-item>

          <s-form-item
            label="可空"
            name="allowNull"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.allowNull" check-value="1" un-check-value="0"> </s-switch>
          </s-form-item>
          <s-form-item
            label="排序"
            name="sort"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.sort" check-value="1" un-check-value="0"> </s-switch>
          </s-form-item>
          <s-form-item
            label="添加"
            name="add"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.add" check-value="1" un-check-value="0"> </s-switch>
          </s-form-item>
          <s-form-item
            label="编辑"
            name="update"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.update" check-value="1" un-check-value="0"> </s-switch>
          </s-form-item>

          <s-form-item
            label="查询"
            name="query"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.query" check-value="1" un-check-value="0"> </s-switch>
          </s-form-item>

          <s-form-item
            label="列表返回"
            name="listReturn"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.listReturn" check-value="1" un-check-value="0"> </s-switch>
          </s-form-item>
          <s-form-item
            label="详情返回"
            name="detailReturn"
            size="custom"
            :options="{ labelWidth: 12, wrapperWidth: 8, offsetWidth: 4, total: 6 }"
          >
            <s-switch v-model="formValue.detailReturn" check-value="1" un-check-value="0">
            </s-switch>
          </s-form-item>
          <s-form-item
            v-if="formValue.query == '1'"
            label="查询方式"
            name="queryType"
            :rules="[{ required: true, message: '不能为空' }]"
          >
            <s-select v-model="formValue.queryType" code="MATCH_TYPE" placeholder="查询方式">
            </s-select>
          </s-form-item>
        </s-form>
      </s-scroll>
    </div>
  </s-modal>
</template>

<script>
import { getCurrentInstance, ref } from 'vue'
export default {
  name: 'BatchAdd',
  emits: ['success'],
  setup(prop, { emit }) {
    const formValue = ref({})
    const visible = ref(false)
    const form = ref(null)
    const save = () => {
      const formRef = form.value
      formRef.validate((values) => {
        visible.value = false
        emit('success', values)
      })
    }
    const open = () => {
      visible.value = true
      formValue.value = {
        startIndex: 1,
        fieldNum: 10,
        name: '备用',
        key: 'remark',
        dbName: 'REMARK',
        javaType: 'String',
        dbType: 'varchar',
        dbLength: 255,
        allowNull: '1',
        sort: '1',
        add: '1',
        update: '1',
        query: '1',
        listReturn: '1',
        detailReturn: '1',
        queryType: '6'
      }
    }
    return {
      formValue,
      visible,
      form,
      save,
      open
    }
  }
}
</script>

<style lang="less" scoped></style>
